<?php
/**
  * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说 图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24 0024
 * Time: 下午 2:14
 */
defined("IN_IA")or exit("Access denied");
global $_GPC,$_W;
$uniacid=$_GPC['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"getLandData";
$uid=$_GPC['uid'];

if($op=='getLandData'){
    $request=array();
    $landData=pdo_getall('cqkundian_farm_land_type',array('uniacid'=>$uniacid),'','','rank asc');
    for($i=0;$i<count($landData);$i++){
        $landData[$i]['slide']=unserialize($landData[$i]['slide']);
        //获取该分类下的土地
        $land=pdo_getall('cqkundian_farm_land',array('uniacid'=>$uniacid,'type_id'=>$landData[$i]['id']),'','','rank asc');
        for($j=0;$j<count($land);$j++){
            $land[$j]['selectArea']=0;
            $limitData=pdo_getall('cqkundian_farm_land_buy_limit',array('uniacid'=>$uniacid,'lid'=>$land[$j]['id']));
            $minPrice=array();
            foreach ($limitData as $value){
                $minPrice[]=$value['price'];
            }
            if(!empty($minPrice)){
                $land[$j]['minPrice']=min($minPrice);
            }
        }
        $landData[$i]['land']=$land;
    }
    $request['landData']=$landData;
    echo json_encode($request);die;
}

//获取租地年限=信息
if($op=='getPayLand'){
    $request=array();
    $selectLand=json_decode($_POST['selectLand']);
    $total_price=0;
    foreach ($selectLand as $key=>$value){
        $landLimit=pdo_getall('cqkundian_farm_land_buy_limit',array('status'=>1,'uniacid'=>$uniacid,'lid'=>$value->id),'','','rank asc');
        $value->landLimit=$landLimit;
        $day=array();
        for($j=0;$j<count($landLimit);$j++){
            $day[]=$landLimit[$j]['day'];
        }
        $value->day=$day;
        $value->select_index=0;
        $total_price+=$value->selectArea*$value->landLimit[$value->select_index]['price']*$value->landLimit[$value->select_index]['day'];
    }
    $request['selectLand']=$selectLand;
    $request['total_price']=$total_price;
    //规则
    $farmRule=pdo_get('cqkundian_farm_set',array('ikey'=>'farm_rule','uniacid'=>$uniacid));
    $request['farmRule']=$farmRule;

    //查看用户是否有优惠券
    $couponCount=0;
    $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>0));
    if(!empty($userCoupon)) {
        for ($i = 0; $i < count($userCoupon); $i++) {
            $coupon = pdo_get('cqkundian_farm_shop_coupon', array('uniacid' => $uniacid, 'id' => $userCoupon[$i]['cid'], 'type' => 4));
            //优惠券是否过期
            if ($coupon['expiry_date'] == 1) {
                if (($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)) > time()) {
                    if ($coupon['low_cash_price'] < $total_price) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            } else {
                if (time() > $coupon['begin_time'] && time() < $coupon['end_time']) {
                    if ($coupon['low_cash_price'] < $total_price) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            }
        }
    }
    $request['couponCount']=$couponCount;
    echo json_encode($request);die;
}

//购买土地生成订单
if($op=='addLandOrder'){
    $land_id=explode('_',$_GPC['land_id']);             //土地ID
    $land_count=explode('_',$_GPC['land_count']);       //购买数量
    $land_limit_id=explode('_',$_GPC['land_limit_id']); //购买年限
    $total_price=$_GPC['total_price'];      //订单总价

    $coupon_id=$_GPC['coupon_id'];
    $coupon_price=$_GPC['coupon_price'];
    include 'function.inc.php';
    $farmSetData=getFarmSetData(array('is_open_distribution','distribution_one_price','distribution_two_price'),array('uniacid'=>$uniacid));
    $inserOrder=array(
        'order_number'=>rand(100,999).time().rand(100,999),
        'uid'=>$uid,
        'total_price'=>$total_price,
        'create_time'=>time(),
        'status'=>0,
        'uniacid'=>$uniacid,
        'body'=>'土地购买',
        'username'=>$_GPC['username'],
        'phone'=>$_GPC['phone'],
        'coupon_id'=>$coupon_id,
        'coupon_price'=>$coupon_price,

    );

    //是否开启分销
    if($farmSetData['is_open_distribution']==1){
        $user=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$uid));
        //当前下单用户的一级分销商
        if($user['one_distributor']!=0) {
            $inserOrder['is_price']=1;
            $one_sale = pdo_get('cqkundian_farm_user', array('uniacid' => $uniacid, 'uid' => $user['one_distributor']));
            $inserOrder['one_price']=round($inserOrder['total_price']*($farmSetData['distribution_one_price']/100),2);
            if($one_sale['one_distributor']!=0){
                $inserOrder['two_price']=round($inserOrder['total_price']*($farmSetData['distribution_two_price']/100),2);
            }
        }
    }

    $order_res=pdo_insert('cqkundian_farm_land_order',$inserOrder);
    $order_id=pdo_insertid();
    $detail_res=0;
    //插入订单详情页
    for($i=0;$i<count($land_id);$i++){
        $landData=pdo_get('cqkundian_farm_land',array('id'=>$land_id[$i],'uniacid'=>$uniacid));
        $limitData=pdo_get('cqkundian_farm_land_buy_limit',array('id'=>$land_limit_id[$i],'uniacid'=>$uniacid));
        $orderDetail=array(
            'order_id'=>$order_id,
            'land_price'=>$landData['price'],
            'day_price'=>$limitData['price'],
            'land_count'=>$land_count[$i],
            'day'=>$limitData['day'],
            'lid'=>$land_id[$i],
            'land_name'=>$landData['land_name'],
            'cover'=>$landData['cover'],
            'uniacid'=>$uniacid,
        );
        $detail_res+=pdo_insert('cqkundian_farm_land_order_detail',$orderDetail);
    }
    if($order_res && $detail_res){
        if($coupon_id && $coupon_price){
            //修改优惠券使用
            pdo_update('cqkundian_farm_user_coupon',array('status'=>1),array('uniacid'=>$uniacid,'uid'=>$uid,'cid'=>$coupon_id));
        }
        echo json_encode(array('code'=>1,'order_id'=>$order_id));die;
    }else{
        echo json_encode(array('code'=>2));die;
    }
}

//购买土地支付回调
if($op=='notify_land'){
    $order_id=$_GPC['order_id'];
    $prepay_id_str=$_GPC['prepay_id'];
    $orderData=pdo_get('cqkundian_farm_land_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    if($orderData['status']==1) {
        include 'function.inc.php';
        if($orderData['is_price']==1){
            saleSendPirce($orderData,$uniacid,4);
            pdo_update('cqkundian_farm_land_order',array('is_price'=>2),array('uniacid'=>$uniacid,'id'=>$order_id));
        }
        updateMineLand($order_id, $uniacid);
        insertIntegral($uniacid, $order_id);
        //向用户推送消息
        $prepay_id = explode('=', $prepay_id_str);
        $wxData = pdo_get('cqkundian_farm_wx_set', array('uniacid' => $uniacid));
        $page = '/kundian_farm/pages/user/land/personLand/index';
        $res_user_send = send_msg_to_user($orderData, $prepay_id[1], $_W['openid'], $uniacid, $page);
        //给店家推送消息
        $wx_openid = unserialize($wxData['get_openid']);
        for ($i = 0; $i < count($wx_openid); $i++) {
            $res_send_shop = send_other_template_message($wx_openid[$i], $orderData, $uniacid);
        }
        echo json_encode(array('code' => 1));
        die;
    }else{
        echo json_encode(array('code' => 2));
        die;
    }
}

//获取我的土地
if($op=='getMineLand'){
    $request=array();
    $current=$_GPC['current'];
    $condition=array();
    if($current==2){
        $condition['status']=1;
    }elseif ($current==3){
        $condition['status']=0;
    }
    if(!empty($_GPC['page'])){
        $page=intval($_GPC['page'])+1;
    }else{
        $page=1;
    }
    $condition['uniacid']=$uniacid;
    $condition['uid']=$uid;
    $landData=pdo_getall('cqkundian_farm_land_mine',$condition,'','','create_time desc',array($page,10));
    for($i=0;$i<count($landData);$i++){
        $land=pdo_get('cqkundian_farm_land',array('id'=>$landData[$i]['lid'],'uniacid'=>$uniacid));
        $landData[$i]['land_name']=$land['land_name'];
        $landData[$i]['cover']=$land['cover'];
        $landData[$i]['exprie_time']=date("Y-m-d H:i:s",$landData[$i]['exprie_time']);
    }
    $request['landData']=$landData;
    echo json_encode($request);die;
}

//获取种植土地详情
if($op=='getMineLandDetail'){
    $request=array();
    $lid=$_GPC['lid'];
    $mineLand=pdo_get('cqkundian_farm_land_mine',array('id'=>$lid,'uniacid'=>$uniacid,'uid'=>$uid));
    $landData=pdo_get('cqkundian_farm_land',array('id'=>$mineLand['lid'],'uniacid'=>$uniacid));
    $mineLand['land_name']=$landData['land_name'];
    $mineLand['cover']=$landData['cover'];
    $mineLand['seed_area']=0;
    //获取监控地址
    if($landData['live_id']!=0){
        $liveData=pdo_get('cqkundian_farm_live',array('uniacid'=>$uniacid,'id'=>$landData['live_id']));
        $mineLand['live_src']=$liveData['src'];
    }
    //获取种子信息
    $sendMine=pdo_getall('cqkundian_farm_send_mine',array('lid'=>$lid,'uniacid'=>$uniacid));
    for($i=0;$i<count($sendMine);$i++){
        $day=floor((time()-$sendMine[$i]['seed_time'])/60/60/24);
        $sendMine[$i]['day']=$day;
        if($sendMine[$i]['status']==1 || $sendMine[$i]['status']==2){
            pdo_update('cqkundian_farm_send_mine',array('day'=>$day),array('id'=>$sendMine[$i]['id'],'uniacid'=>$uniacid));
        }
        if($sendMine[$i]['status']==1){
            $total_day=floor(($sendMine[$i]['expect_time']-$sendMine[$i]['seed_time'])/60/60/24);  //获取总共需要的天数
            $ratio=($day/$total_day)*100;
            $sendMine[$i]['ratio']=$ratio;
        }
        $mineLand['seed_area']+=$sendMine[$i]['count']; //获取已种植的面积
    }
    $request['sendMine']=$sendMine;
    $request['mineLand']=$mineLand;
    //获取跟踪信息
    $landStatus=pdo_getall('cqkundian_farm_send_status',array('lid'=>$lid,'uniacid'=>$uniacid),'','','create_time desc',array(1,10));
    for($i=0;$i<count($landStatus);$i++){
        $landStatus[$i]['create_time']=date("Y-m-d H:i",$landStatus[$i]['create_time']);
        $landStatus[$i]['src']=unserialize($landStatus[$i]['src']);
    }
    $request['landStatus']=$landStatus;
    //获取配置信息
    include 'function.inc.php';
    $farmData=getFarmSetData(array('is_open_webthing'),array('uniacid'=>$uniacid));
    $request['setData']=$farmData;
    echo json_encode($request);die;
}

//获取种子列表
if($op=='getSendList'){
    $request=array();
    $page=empty($_GPC['page'])? 1:intval($_GPC['page'])+1;
    $lid=$_GPC['lid'];
    $mineLand=pdo_get('cqkundian_farm_land_mine',array('id'=>$lid,'uniacid'=>$uniacid,'uid'=>$uid));
    $mineLand['seed_area']=0;
    $mineSend=pdo_getall('cqkundian_farm_send_mine',array('lid'=>$lid,'uniacid'=>$uniacid));
    for($i=0;$i<count($mineSend);$i++){
        $mineLand['seed_area']+=$mineSend[$i]['count'];
    }
    $request['mineLand']=$mineLand;
    $landData=pdo_get('cqkundian_farm_land',array('id'=>$mineLand['lid'],'uniacid'=>$uniacid));
    $seed_id=explode(',',$landData['seed']);
    $send=pdo_getall('cqkundian_farm_send',array('is_putaway'=>1,'uniacid'=>$uniacid,'id in'=>$seed_id),'','','rank asc',array($page,10));
    for($i=0;$i<count($send);$i++){
        $send[$i]['selected']=false;
        $send[$i]['num']=1;
    }
    $request['send']=$send;
    echo json_encode($request);die;
}

//获取种子详细信息
if($op=='getSendDetail'){
    $request=array();
    $sid=$_GPC['sid'];
    $sendDetail=pdo_get('cqkundian_farm_send',array('id'=>$sid,'uniacid'=>$uniacid));
    $sendDetail['send_slide']=unserialize($sendDetail['send_slide']);
    $request['sendDetail']=$sendDetail;
    echo json_encode($request);die;
}

//购买种子
if($op=='addSendOrder'){
    $sid=explode('_',$_GPC['sid']);
    $count=explode("_",$_GPC['count']);
    $total_price=$_GPC['total_price'];

    $coupon_id=$_GPC['coupon_id'];
    $coupon_price=$_GPC['coupon_price'];

    $inserSendOrder=array(
        'order_number'=>rand(100,999).time().rand(100,999),
        'uid'=>$uid,
        'uniacid'=>$uniacid,
        'total_price'=>$total_price,
        'create_time'=>time(),
        'status'=>0,
        'body'=>'购买种子',
        'coupon_id'=>$coupon_id,
        'coupon_price'=>$coupon_price,
    );
    $order_send_res=pdo_insert('cqkundian_farm_send_order',$inserSendOrder);
    $order_id=pdo_insertid();
    $res_detail=0;
    for($i=0;$i<count($sid);$i++){
        $sendData=pdo_get('cqkundian_farm_send',array('id'=>$sid[$i],'uniacid'=>$uniacid));
        $insertSendDetail=array(
            'sid'=>$sid[$i],
            'count'=>$count[$i],
            'order_id'=>$order_id,
            'send_name'=>$sendData['send_name'],
            'cover'=>$sendData['cover'],
            'price'=>$sendData['price'],
            'uniacid'=>$uniacid,
        );

        $res_detail+=pdo_insert('cqkundian_farm_send_order_detail',$insertSendDetail);
    }
    if($order_send_res && $res_detail){
        if($coupon_id && $coupon_price){
            //修改优惠券使用
            pdo_update('cqkundian_farm_user_coupon',array('status'=>1),array('uniacid'=>$uniacid,'uid'=>$uid,'cid'=>$coupon_id));
        }
        echo json_encode(array('code'=>1,'order_id'=>$order_id));die;
    }else{
        echo json_encode(array('code'=>2));die;
    }
}

//购买种子支付回调
if($op=='notify_send'){
    $order_id=$_GPC['order_id'];
    $lid=$_GPC['lid'];
    $prepay_id_str=$_GPC['prepay_id'];
    $orderData=pdo_get('cqkundian_farm_send_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    if($orderData['status']==1) {
        updateSend($order_id, $uniacid, $lid);
        insertIntegralSeed($uniacid, $order_id);
        //向用户推送消息
        $prepay_id = explode('=', $prepay_id_str);
        $wxData = pdo_get('cqkundian_farm_wx_set', array('uniacid' => $uniacid));
        include 'function.inc.php';
        $res_user_send = send_msg_to_user($orderData, $prepay_id[1], $_W['openid'], $uniacid, $page);
        //给店家推送消息
        $wx_openid = unserialize($wxData['get_openid']);
        for ($i = 0; $i < count($wx_openid); $i++) {
            $res_send_shop = send_other_template_message($wx_openid[$i], $orderData, $uniacid);
        }
        echo json_encode(array('code' => 1));
        die;
    }else{
        echo json_encode(array('code' => 2));
        die;
    }
}

if($op=='ceshi'){
    $order_id=$_GPC['order_id'];
    $lid=$_GPC['lid'];
    $prepay_id_str=$_GPC['prepay_id'];
    $orderData=pdo_get('cqkundian_farm_send_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    if($orderData['status']==1) {
        updateSend($order_id, $uniacid, $lid);
        insertIntegralSeed($uniacid, $order_id);
        //向用户推送消息
        $prepay_id = explode('=', $prepay_id_str);
        $wxData = pdo_get('cqkundian_farm_wx_set', array('uniacid' => $uniacid));
        include 'function.inc.php';
        $res_user_send = send_msg_to_user($orderData, $prepay_id[1], $_W['openid'], $uniacid, $page);
        //给店家推送消息
        $wx_openid = unserialize($wxData['get_openid']);
        for ($i = 0; $i < count($wx_openid); $i++) {
            $res_send_shop = send_other_template_message($wx_openid[$i], $orderData, $uniacid);
        }
        echo json_encode(array('code' => 1));
        die;
    }else{
        echo json_encode(array('code' => 2));
        die;
    }
}

/**
 * 添加积分记录
 * @param $uniacid
 * @param $uid
 * @param $score
 */
function insertIntegral($uniacid,$order_id){
    $orderData=pdo_get('cqkundian_farm_land_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    $userData=pdo_get('cqkundian_farm_user',array('uid'=>$orderData['uid'],'uniacid'=>$uniacid));
    $score=round($orderData['total_price']/$aboutData['pay_integral']);
    pdo_update('cqkundian_farm_user',array('score +='=>$score),array('uid'=>$orderData['uid'],'uniacid'=>$uniacid));
    $insertData=array(
        'uid'=>$orderData['uid'],
        'uniacid'=>$uniacid,
        'create_time'=>time(),
        'score_type'=>1,
        'score'=>$score,
        'body'=>$orderData['body'],
        'now_score'=>$userData['score']+$score,
    );
    pdo_insert('cqkundian_farm_integral_record',$insertData);
}
/**
 * 添加积分记录
 * @param $uniacid
 * @param $uid
 * @param $score
 */
function insertIntegralSeed($uniacid,$order_id){
    $orderData=pdo_get('cqkundian_farm_send_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    $userData=pdo_get('cqkundian_farm_user',array('uid'=>$orderData['uid'],'uniacid'=>$uniacid));
    $score=round($orderData['total_price']/$aboutData['pay_integral']);
    pdo_update('cqkundian_farm_user',array('score +='=>$score),array('uid'=>$orderData['uid'],'uniacid'=>$uniacid));
    $insertData=array(
        'uid'=>$orderData['uid'],
        'uniacid'=>$uniacid,
        'create_time'=>time(),
        'score_type'=>1,
        'score'=>$score,
        'body'=>$orderData['body'],
        'now_score'=>$userData['score']+$score,
    );
    pdo_insert('cqkundian_farm_integral_record',$insertData);
}

/**
 * 更新种子信息
 * @param $order_id
 * @param $uniacid
 */
function updateSend($order_id,$uniacid,$lid){
    $aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    $orderData=pdo_get('cqkundian_farm_send_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $orderDetail=pdo_getall('cqkundian_farm_send_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    pdo_update('cqkundian_farm_user',array('score +='=>$orderData['total_price']/$aboutData['pay_integral']),array('uid'=>$orderData['uid'],'uniacid'=>$uniacid));
    for($i=0;$i<count($orderDetail);$i++){
        $insertSend=array(
            'uid'=>$orderData['uid'],
            'order_id'=>$order_id,
            'sid'=>$orderDetail[$i]['sid'],
            'status'=>0,
            'day'=>0,
            'send_name'=>$orderDetail[$i]['send_name'],
            'count'=>$orderDetail[$i]['count'],
            'uniacid'=>$uniacid,
            'lid'=>$lid,
        );

        pdo_insert('cqkundian_farm_send_mine',$insertSend);
    }
    //更新土地种植信息
    pdo_update('cqkundian_farm_land_mine',array('status'=>1),array('id'=>$lid,'uniacid'=>$uniacid));
}

/**
 * 更新土地信息
 * @param $order_id
 * @param $uniacid
 */
function updateMineLand($order_id,$uniacid){
    $orderData=pdo_get('cqkundian_farm_land_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $orderDetail=pdo_getall('cqkundian_farm_land_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    for($i=0;$i<count($orderDetail);$i++){
        $insertMine=array(
            'uid'=>$orderData['uid'],
            'lid'=>$orderDetail[$i]['lid'],
            'count'=>$orderDetail[$i]['land_count'],
            'exprie_time'=>$orderDetail[$i]['day']*24*60*60+time(),
            'day'=>$orderDetail[$i]['day'],
            'create_time'=>time(),
            'status'=>0,
            'send_day'=>0,
            'order_id'=>$order_id,
            'uniacid'=>$uniacid,
        );
        pdo_insert('cqkundian_farm_land_mine',$insertMine);

        $update_where=array(
            'area +='=>$orderDetail[$i]['land_count'],
            'residue_area -='=>$orderDetail[$i]['land_count'],
        );
        pdo_update('cqkundian_farm_land',$update_where,array('id'=>$orderDetail[$i]['lid'],'uniacid'=>$uniacid));
    }
}











